package com.ccc.dingtalk.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * Json序列化配置
 *
 * @author superC
 * @date 2022/3/24
 * @version 1.0.0
 */
@JsonComponent
public class JsonSerializerConfig {

    private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private static final String YYYY_MM_DD = "yyyy-MM-dd";
    private static final String HH_MM_SS = "HH:mm:ss";

    @Bean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        /*
         * 忽略value为null 时 key的输出
         */
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        /*
         * 设置为东八区的时区。此处配置了，@JsonFormat注解则可省略时区配置；
         */
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        /*
        目的：解决Long类型超过16位时传递给前端丢失精度，因为js中得数字类型不能包含所有的java long值
        解决方案：序列换成json时,将所有的long变成string
         */
        SimpleModule module = new SimpleModule();
        // 对象类型Long 例：private Long id;
        module.addSerializer(Long.class, ToStringSerializer.instance);
        /// 基础类型long 例：private long current;（因为BaseService中使用long类型 表示current、size，则注释）
//        module.addSerializer(Long.TYPE, ToStringSerializer.instance);

        /*
            jackson的日期时间序列化、反序列化（jdk8新对象）
                1. 序列化是将实体对象转为json串；
                2. 反序列化是将json串转换为实体对象。
         */
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS)));
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS)));
        module.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(YYYY_MM_DD)));
        module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(YYYY_MM_DD)));
        module.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(HH_MM_SS)));
        module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(HH_MM_SS)));

        objectMapper.registerModule(module);
        return objectMapper;
    }

}